//
//  TYSwitchView.m
//  TYUIKit_Example
//
//  Created by 谢航宇_WORK on 2022/3/17.
//  Copyright © 2022 谢航宇. All rights reserved.
//

#import "TYSwitchView.h"
#import "Masonry.h"
#import "UIColor+TYColor.h"
@interface TYSwitchViewCell : UICollectionViewCell
@property (nonatomic,retain) UILabel * titleLab;
@end

@implementation TYSwitchViewCell
- (UILabel *)titleLab {
    if (!_titleLab) {
        __weak typeof(self)weakSelf = self;
        _titleLab = [[UILabel alloc]init];
        _titleLab.textAlignment = NSTextAlignmentCenter;
        [self addSubview:_titleLab];
        [_titleLab mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.bottom.top.mas_equalTo(weakSelf);
        }];
    }
    return _titleLab;
}
@end

@interface TYSwitchView () <UICollectionViewDelegate,UICollectionViewDataSource>
@property (nonatomic,retain) UICollectionView * collectView;
@end

@implementation TYSwitchView
- (void)setDelegate:(id <TYSwitchViewDelegate,UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>)delegate {
    _delegate = delegate;
    [self.collectView reloadData]; 
}

- (void)setType:(TYSwitchViewSeparatorType)type {
    _type = type;
    [self.collectView reloadData];
}

- (UICollectionView *)collectView {
    if (!_collectView) {
        __weak typeof(self)weakSelf = self;
        UICollectionViewFlowLayout * flowLayout = [[UICollectionViewFlowLayout alloc]init];
        flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
        _collectView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height) collectionViewLayout:flowLayout];
        _collectView.delegate = self;
        _collectView.backgroundColor = [UIColor clearColor];
        _collectView.dataSource = self;
        _collectView.bounces = NO;
        _collectView.showsVerticalScrollIndicator = NO;
        _collectView.showsHorizontalScrollIndicator = NO;
        [_collectView registerClass:[TYSwitchViewCell class] forCellWithReuseIdentifier:@"TYSwitchViewCell"];
        [self addSubview:_collectView];
        [_collectView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(weakSelf);
            make.left.mas_equalTo(weakSelf);
            make.right.mas_equalTo(weakSelf);
            make.bottom.mas_equalTo(weakSelf);
        }];
    }
    return _collectView;
}

- (void)registerClass:(nullable Class)cellClass forCellWithReuseIdentifier:(NSString *)identifier {
    [self.collectView registerClass:cellClass forCellWithReuseIdentifier:identifier]; 
}

- (void)reloadData {
    [self.collectView reloadData];
}

#pragma mark ---瀑布流代理方法
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return [self.delegate numberOfRowOfSwitchHeaderView:self];
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return 1;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    UICollectionViewCell * cell = nil;
    if ([self.delegate respondsToSelector:@selector(collectionView:cellForItemAtIndexPath:)] && self.type != TYSwitchViewSeparatorTypeAutoSize) {
        cell = [self.delegate collectionView:collectionView cellForItemAtIndexPath:indexPath];
    }else {
        TYSwitchViewCell * switchCell = [collectionView dequeueReusableCellWithReuseIdentifier:@"TYSwitchViewCell" forIndexPath:indexPath];
        switchCell.titleLab.attributedText = [self.delegate switchHeaderView:self titleForRow:indexPath.row isSelected:self.index == indexPath.row];
        cell = switchCell;
    }
    
    if ([self.delegate respondsToSelector:@selector(switchHeaderView:backgroundView:forRow:isSelected:)]) {
        if (!cell.backgroundView) {
            cell.backgroundView = [[UIImageView alloc]init];
        }
        [self.delegate switchHeaderView:self backgroundView:(UIImageView *)cell.backgroundView forRow:indexPath.row isSelected:self.index == indexPath.row];
    }
    
    return cell;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    if (self.type == TYSwitchViewSeparatorTypeDivide) {
        return CGSizeMake((self.frame.size.width - 1)/[self.delegate numberOfRowOfSwitchHeaderView:self], self.frame.size.height);
    }
    if (self.type == TYSwitchViewSeparatorTypeAutoSize) {
        CGFloat width = 88;
        NSAttributedString * attr = [self.delegate switchHeaderView:self titleForRow:indexPath.row isSelected:self.index == indexPath.row];
        if (attr.size.width + 24 > width) {
            width = attr.size.width + 24;
        }
        return CGSizeMake(width, self.frame.size.height);
    }
    if ([self.delegate respondsToSelector:@selector(collectionView:layout:sizeForItemAtIndexPath:)]) {
        return [self.delegate collectionView:collectionView layout:collectionViewLayout sizeForItemAtIndexPath:indexPath];
    }
    return CGSizeMake((self.frame.size.width -1)/[self.delegate numberOfRowOfSwitchHeaderView:self], self.frame.size.height);
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section{
    return 0;
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section{
    return 0;
}

- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{
    return UIEdgeInsetsMake(0, 0, 0, 0);
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    if (self.selectComplete) {
        self.selectComplete(indexPath.row, self.index == indexPath.row);
    }
    self.index = indexPath.row;
    [collectionView reloadData];
    //当点击当前显示cell的前两个或后两个，，跳转居中
    [collectionView layoutIfNeeded];
    if (collectionView.indexPathsForVisibleItems.count - [collectionView.indexPathsForVisibleItems indexOfObject:indexPath] <= 2 || [collectionView.indexPathsForVisibleItems indexOfObject:indexPath] < 2) {
        [collectionView scrollToItemAtIndexPath:indexPath atScrollPosition:(UICollectionViewScrollPositionCenteredHorizontally) animated:YES];
    }
}

@end
